home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-22 | 5.2 KB | 221 lines | [TEXT/CWIE] |
- #include "DialogUtils.h"
-
- /* Much of this code is from C.K. Haun's DialogBits snippet, but of course has
- been tweaked for my own purposes */
-
-
-
- /* This filter proc only allows numeric input, and also does the standard key
- filtering */
- pascal Boolean NumFilter(DialogPtr dptr, EventRecord *event, short *item)
- {
- char theKey;
- WindowPtr temp;
- Boolean returnVal = false;
-
- GetPort(&temp);
- SetPort(dptr);
-
- /* Change the cursor to an I Beam if it's over the active editText item */
- IBeamIt(dptr);
-
- /* Standard key filtering */
- returnVal = StdKeyFilter(dptr, event, item);
-
- /* if that didn't handle it... */
- if(returnVal == false)
- {
- /* We're only allowing numeric characters */
- if ((event->what == keyDown) || (event->what == autoKey))
- {
- theKey = event->message & charCodeMask;
- if(theKey > kLastCntrlKey && theKey < kDeleteKey) /* Printable Ascii? */
- {
- if (theKey < '0' || theKey > '9') /* not a number? */
- {
- SysBeep(1); /* complain a little */
- returnVal = true;
- }
- }
- else
- returnVal = false;
- }
- }
- SetPort(temp);
- return(returnVal);
- }
-
- /* Standard key filtering: return or enter hit the OK button, escape hits cancel.
- Also Hilite the button for visual feedback. */
- pascal Boolean StdKeyFilter(DialogPtr dptr, EventRecord *event, short *item)
- {
- long tilticks;
- char theKey;
- Boolean returnVal = false;
-
- if ((event->what == keyDown) || (event->what == autoKey))
- {
- theKey = event->message & charCodeMask;
- switch (theKey)
- {
- /* return or enter hits the OK button */
- case kReturnKey:
- case kEnterKey:
- *item = kOKButton;
- /* now we need to invert the button */
- HiliteControl(SnatchHandle(dptr, kOKButton), inButton);
- Delay(8, &tilticks); /* wait about 8 ticks so they can see it */
- HiliteControl(SnatchHandle(dptr, kOKButton), 0);
- returnVal = true;
- break;
-
- /* Escape hits the cancel button */
- case kEscKey:
- *item = kCancelButton;
- HiliteControl(SnatchHandle(dptr, kCancelButton), inButton);
- Delay(8, &tilticks); /* wait about 8 ticks so they can see it */
- HiliteControl(SnatchHandle(dptr, kCancelButton), 0);
- returnVal = true;
- break;
-
- default:
- break;
- }
- }
- return returnVal;
- }
-
- void IBeamIt(WindowPtr dwind)
- {
- Point thePt;
- short kind;
- Handle itmhndl;
- Rect rect;
- short itemNum;
-
- /* first get the current edit line out of the dialog record */
- itemNum = ((DialogPeek)dwind)->editField + 1; /* always stored 1 less */
- GetDItem(dwind, itemNum, &kind, &itmhndl, &rect);
- GetMouse(&thePt);
- if (PtInRect(thePt, &rect))
- {
- SetCursor(*(GetCursor(iBeamCursor)));
- }
- else
- {
- InitCursor();
- }
- }
-
- void ShortToDlog(short val, DialogPtr dptr, short item)
- {
- short kind;
- Handle itmhndl;
- Rect rect;
- Str255 tempstr;
-
- NumToString((long)val, tempstr);
- GetDItem(dptr, item, &kind, &itmhndl, &rect);
- SetIText(itmhndl, tempstr);
- }
-
- short DlogToShort(DialogPtr dptr, short itmnum)
- {
- Handle itmhndl;
- Rect rect;
- Str255 tempstr;
- short kind;
- long temp;
-
- GetDItem(dptr, itmnum, &kind, &itmhndl, &rect);
- GetIText(itmhndl, tempstr);
- StringToNum(tempstr, &temp);
- return (short)temp;
- }
-
- short FutureNumber(DialogPtr dptr, short itmnum, char nextNum)
- {
- Handle itmhndl;
- Rect rect;
- Str255 tempstr, numNow;
- short kind;
- short srcCnt, dstCnt, charCnt, selStart, selEnd;
- long num;
-
- /* Get the text we have so far */
- GetDItem(dptr, itmnum, &kind, &itmhndl, &rect);
- GetIText(itmhndl, numNow);
-
- /* Get the selection range */
- selStart = (**(((DialogPeek)dptr)->textH)).selStart;
- selEnd = (**(((DialogPeek)dptr)->textH)).selEnd;
-
- /* First copy the string before the selection starts */
- charCnt = 0;
- for(srcCnt = 1; srcCnt <= selStart; srcCnt++)
- {
- tempstr[srcCnt] = numNow[srcCnt];
- charCnt++;
- }
- /* Then add the key char */
- tempstr[srcCnt++] = nextNum;
- charCnt++;
-
- dstCnt = srcCnt;
-
- /* Finally the rest of the string, after the selection */
- for(srcCnt = selEnd + 1; srcCnt <= numNow[0]; srcCnt++)
- {
- tempstr[dstCnt++] = numNow[srcCnt];
- charCnt++;
- }
- tempstr[0] = charCnt;
- StringToNum(tempstr, &num);
- return (short)num;
- }
-
- pascal void BtnItem(DialogPtr dptr, short item)
- {
- short type;
- Rect rect;
- Handle hndl;
- PenState old;
-
- GetPenState(&old);
- PenSize(3, 3);
- GetDItem(dptr, item, &type, &hndl, &rect);
- InsetRect(&rect, -4, -4);
- FrameRoundRect(&rect, 16, 16);
- SetPenState(&old);
- }
-
- /* Gets the ControlHandle for the item you want in the dialog box thebox.
- Handy for setting checkboxes and radio buttons */
- ControlHandle SnatchHandle(DialogPtr thebox, short theGetItem)
- {
- short itemtype;
- Rect itemrect;
- Handle thandle;
-
- GetDItem(thebox, theGetItem, &itemtype, &thandle, &itemrect);
- return((ControlHandle)thandle);
- }
-
- void EnableControl(DialogPtr dptr, short itemNum)
- {
- ControlHandle itemHandle;
-
- itemHandle = SnatchHandle(dptr, itemNum);
- if(itemHandle != nil)
- HiliteControl(itemHandle, 0);
- }
-
- void DisableControl(DialogPtr dptr, short itemNum)
- {
- ControlHandle itemHandle;
-
- itemHandle = SnatchHandle(dptr, itemNum);
- if(itemHandle != nil)
- HiliteControl(itemHandle, 255);
- }
-